// 1219.[Tarjan塔杨最短路径/双向连通图dijkstra/领接矩阵] 最小路由时延和
// https://oj.rnd.huawei.com/problems/1219/details
// 某两个路由器之间最多只有一条网线双向连通，其通信时延为
// t（连通双向，时延相等） 现给定 m 个连通关系和时延，以及指定的两个路由器 Ri 和
// Rj，请计算 Ri 和 Rj 之间的最小时延（各时延之和）
//输入 6 6
// 1 2 2
// 1 3 3
// 2 4 4
// 2 5 5
// 4 6 3
// 5 6 1
// 6 1        // 6 -> 1
//输出 8

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const static int VALUE_MAX = 0x3fffffff;
typedef pair<int, int> Router;  // from  <toRouteId, dealy>
typedef pair<int, int> ToNode;  // to, delay
typedef pair<int, int> FromTo;  // from, to
#define ROUTE_ID 0
#define ROUTE_DELAY 1
#define ToNode_ID 0
#define ToNode_DELAY 1

struct Cmp_Greater {  // 递增,后面的数更大
  bool operator()(const Router& rt1, const Router& rt2) {
    return get<ROUTE_DELAY>(rt1) > get<ROUTE_DELAY>(rt2);
  }
};

// 待实现函数，在此函数中填入答题代码
int GetMinDelay(int routerNum, const vector<vector<int>>& linkDelays,
                int routerRi, int routerRj) {
  // 时延最小的优先出队
  std::priority_queue<Router, vector<Router>, Cmp_Greater> queRouter;
  map<int, set<ToNode>> mapConnection;  // from, to
  set<FromTo> stVisited;
  for (auto ln : linkDelays) {
    //  源、目标、时延三元组
    int from = ln[0];
    int to = ln[1];
    int delay = ln[2];
    // printf("conn=%d->%d,delay=%d\n", from, to, delay);
    mapConnection[from].insert({to, delay});
    if (from == routerRi) {
      queRouter.push({to, delay});
      stVisited.insert({from, to});
      // printf("src=%d,delay=%d\n", from, delay);
    }
    // both ways
    swap(from, to);
    // printf("conn=%d->%d,delay=%d\n", from, to, delay);
    mapConnection[from].insert({to, delay});
    if (from == routerRi) {
      queRouter.push({to, delay});
      stVisited.insert({from, to});
      // printf("src=%d,delay=%d\n", from, delay);
    }
  }
  int res_minDelay = VALUE_MAX;
  while (!queRouter.empty()) {
    Router rt = queRouter.top();
    queRouter.pop();
    int curRoute = get<ROUTE_ID>(rt);
    int delaySum = get<ROUTE_DELAY>(rt);
    // printf("cur=%d,deSum=%d\n", curRoute, delaySum);
    if (routerRj == curRoute) {
      if (delaySum < res_minDelay) {
        res_minDelay = delaySum;
      }
      // printf("Route.end=%d,deSum=%d\n", curRoute, delaySum);
      continue;
    }
    for (auto next : mapConnection[curRoute]) {
      int nextRoute = get<ToNode_ID>(next);
      if (stVisited.count({curRoute, nextRoute}) == 0) {
        queRouter.push({nextRoute, delaySum + get<ToNode_DELAY>(next)});
        stVisited.insert({curRoute, nextRoute});
        // printf("cur=%d -> %d deSum=%d\n", curRoute, nextRoute, delaySum +
        // get<ToNode_DELAY>(next));
      }
    }
  }
  return (res_minDelay == VALUE_MAX) ? -1 : res_minDelay;
}

// 以下为考题输入输出框架，此部分代码不建议改动
inline int ReadInt() {
  int number;
  std::cin >> number;
  return number;
}

inline std::vector<int> ReadVector(int size) {
  std::vector<int> numbers(size);
  for (int i = 0; i < size; ++i) {
    std::cin >> numbers[i];
  }
  return numbers;
}

int main() {
  int routerNum = ReadInt();
  int linkNum = ReadInt();
  vector<vector<int>> linkDelays(linkNum);
  for (auto& oneLink : linkDelays) {
    oneLink = ReadVector(3);  // 3 : 源、目标、时延三元组
  }
  int router1 = ReadInt();
  int router2 = ReadInt();
  std::cout << GetMinDelay(routerNum, linkDelays, router1, router2);

  return 0;
}
